package com.business.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.business.dao.TagItemMapper;
import com.business.dao.TagMapper;
import com.business.domain.Tag;
import com.business.domain.TagExample;
import com.business.domain.TagItem;
import com.business.domain.TagItemExample;
import com.business.domain.TagExample.Criteria;
import com.business.service.TagService;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.ResultVo;

@Service("tagService")
public class TagServiceImpl implements TagService{
	
	private static final Logger logger = LoggerFactory.getLogger(TagServiceImpl.class);
	
	@Resource
	public TagMapper tagMapper;
	@Resource
	public TagItemMapper tagItemMapper;

	@Override
	public RequestPageVo<Tag> getTagList(PageParam param, Tag tag) {
		RequestPageVo<Tag> vo = new RequestPageVo<Tag>();
		
		TagExample example = new TagExample();
		example.setOrderByClause("creatime desc");
		Criteria create = example.createCriteria();
		if(!StringUtils.isEmpty(tag.getName())){
			create.andNameLike("%"+tag.getName()+"%");
		}
		if(!StringUtils.isEmpty(tag.getCode())){
			create.andCodeEqualTo(tag.getCode());
		}
		int count = tagMapper.countByExample(example);
		if(count>0){
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<Tag> list = tagMapper.selectByExample(example);
			for (Tag tag2 : list) {
				TagItemExample itemExample = new TagItemExample();
				itemExample.setOrderByClause("creatime desc");
				com.business.domain.TagItemExample.Criteria createCriteria = itemExample.createCriteria();
				createCriteria.andTagIdEqualTo(tag2.getId());
				List<TagItem> itemList = tagItemMapper.selectByExample(itemExample);
				tag2.setItem(itemList);
			}
			vo.setRows(list);
			vo.setTotal(count);
		}
		return vo;
	}

	@Override
	public ResultVo deleteTag(Integer id) {
		ResultVo result = new ResultVo();
		try {
			//如果标签下面有值就先删除标签值
			TagItemExample example=new TagItemExample();
			com.business.domain.TagItemExample.Criteria criteria=example.createCriteria();
			criteria.andTagIdEqualTo(id);
			tagItemMapper.deleteByExample(example);
			
			tagMapper.deleteByPrimaryKey(id);
			
			result.setSuccess(true);
			result.setErrorMessage("删除成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMessage("删除失败");
		}
		return result;
	}

	@Override
	public ResultVo saveOrUpdateTag(Tag tag) {
		ResultVo result = new ResultVo();
		
		TagExample example =new TagExample();
		Criteria criteria=example.createCriteria();
		criteria.andCodeEqualTo(tag.getCode());
		List<Tag> list=tagMapper.selectByExample(example);
		try {
			if (tag.getId() == null) {//判断属于添加还是修改
				
				if(list!=null&&list.size()>0){//判断编码是否重复
					result.setSuccess(false);
					result.setErrorMessage("该编码已存在，请勿重复添加！");
					return result;
				}else{
					// 添加
					tag.setCreatime(new Date());
					tagMapper.insertSelective(tag);
					result.setSuccess(true);
				}
			} else {
				for(Iterator<Tag> itor=list.iterator();itor.hasNext();){
					Tag t=itor.next();
					if(t.getId()!=tag.getId()){//名称修改了
						if(list.size()>0){
							result.setSuccess(false);
							result.setErrorMessage("该编码已存在，请勿重复！");
							return result;
						}
					}
				}
				tagMapper.updateByPrimaryKeySelective(tag);
				result.setSuccess(true);
			}
		} catch (Exception e) {
			logger.error("updateBrand error", e);
			result.setSuccess(false);
		} 
		return result;
		
	}
	
}
